home *** CD-ROM | disk | FTP | other *** search
/ Aminet 5 / Aminet 5 - March 1995.iso / Aminet / dev / misc / LEDA_high.lha / LEDA-3.1c-high / man / string.tex < prev    next >
Encoding:
Text File  |  1994-08-05  |  5.1 KB  |  142 lines

  1. \bigskip
  2. \bigskip
  3. {\magonebf 2.2 Strings (string)}     
  4.  
  5. Data type $string$ is the LEDA equivalent of $char*$ in \CC. The differences 
  6. to the $char*$-type are that assignment, compare  and concatenation 
  7. operators are defined and that argument passing by value works properly, 
  8. i.e., there is passed a copy of the string and not only a copy of a pointer.
  9. Furthermore a few useful operations for string manipulations are available. 
  10.  
  11.  
  12. \def\name{$string$}
  13. \def\type{$string$}
  14.  
  15. {\bf 1. Creation of a string }
  16.  
  17. a) \create s {}
  18.  
  19. b) \create s (char*\ p)
  20.  
  21. b) \create s (char\ c)
  22.  
  23. introduces a variable $s$ of type $string$. $s$ is initialized with the 
  24. empty string (variant a), the string constant $p$ (variant b), or the 
  25. one-character string ``$c$" (variant c).
  26.  
  27.  
  28. \bigskip
  29. {\bf 2. Operations on a string s}
  30.  
  31. \medskip
  32. \+\cleartabs & \hskip 2.5truecm & \hskip 5truecm &\cr
  33. \+\op int     length {}
  34.                           {returns the length of string $s$}
  35. \smallskip
  36. \+\opa char\&   {int\ i}   
  37.                           {returns the character at position $i$}
  38. \+\nop                    {\precond{$0 \le i \le s$.length()$-$1} }
  39. \smallskip
  40. \+\opf string {int\ i,\ int\ j}
  41.                           {returns the substring of $s$ starting at}
  42. \+\nop                    {position $i$ and ending at position $j$}
  43. \+\nop                    {\precond{$0 \le i \le j \le s$.length()$-$1} }
  44. \smallskip
  45. \+\op string  tail   {int\ i}       
  46.                           {returns the last $i$ characters of $s$ }
  47. \smallskip
  48. \+\op string  head   {int\ i}       
  49.                           {returns the first $i$ characters of $s$ }
  50. \smallskip
  51.  
  52. \+\op int     pos    {string\ s_1}    
  53.                         {returns the first position of $s_1$ in $s$ if $s_1$ is}
  54. \+\nop                  {a substring of $s$, $-1$ otherwise}
  55. \smallskip
  56. \+\op int     pos    {string\ s_1,\ int\ i}    
  57.                           {returns the first position of $s_1$ in $s$ right of}
  58. \+\nop                    {position $i$ (-1 if no such position exists)}
  59. \smallskip
  60. \+\op string  insert {string\ s_1,\ int\ i}
  61.                           {returns $s(0,i-1)$ + $s_1$ + $s(i,s.length()-1)$}
  62. \+\nop                    {\precond{$0 \le i \le s$.length()$-$1} }
  63. \smallskip
  64. \+\op string  replace {string\ s_1,\ string\ s_2,\ int\ i=1} {}
  65. \+\nop                    { returns the string created from $s$ by replacing }
  66. \+\nop                    { the $i$-th occurence of $s_1$ in $s$ by $s_2$ }
  67. \smallskip
  68. \+\op string  replace\_all {string\ s_1,\ string\ s_2} {}
  69. \+\nop                    { returns the string created from $s$ by replacing }
  70. \+\nop                    { all occurences of $s_1$ in $s$ by $s_2$ }
  71. \smallskip
  72. \+\op string  replace {int\ i,\ int\ j,\ string\ s_1} {}
  73. \+\nop                    { returns the string created from $s$ by replacing }
  74. \+\nop                    { $s(i,j)$ by $s_1$ }
  75. \smallskip
  76. \+\op string  replace {int\ i,\ string\ s_1} {}
  77. \+\nop                    { returns the string created from $s$ by replacing }
  78. \+\nop                    { $s[i]$ by $s_1$ }
  79. \smallskip
  80. \+\op string del {string\ s_1,\ int\ i=1}  
  81.                           { returns $s$.replace($s_1,"",i$) }
  82. \smallskip
  83. \+\op string del\_all {string\ s_1}         
  84.                           { returns $s$.replace\_all($s_1,""$) }
  85. \smallskip
  86. \+\op string del {int\ i,\ int\ j}        
  87.                           { returns $s$.replace($i,j,""$) }
  88. \smallskip
  89. \+\op string del {int\ i}                 
  90.                           { returns $s$.replace($i,""$) }
  91. \smallskip
  92. \+\op void   read {istream\  I,\ char\ delim = '\ '} {}
  93. \+\nop                    {reads characters from input stream $I$ into $s$}
  94. \+\nop                    {until the first occurence of character $delim$}
  95. \smallskip
  96. \+\op void   read {char\  delim = '\ '}  
  97.                           { read($cin$,$delim$) }
  98. \smallskip
  99. \+\op void   read\_line {istream\ I}       
  100.                           { read($I$,'$\backslash$n') }
  101. \smallskip
  102. \+\op void   read\_line {}                 
  103.                           { read\_line($cin$) }
  104. \smallskip
  105. \+\opb string    +  s_1  
  106.                           {returns the concatenation of $s$ and $s_1$}
  107. \smallskip
  108. \+\opb string\&  += s_1  
  109.                           {appends $s_1$  to $s$ and returns $s$}
  110. \smallskip
  111. \+\opb bool      == s_1  
  112.                           {true iff $s$ and $s_1$ are equal}
  113. \smallskip
  114. \+\opb bool      != s_1  
  115.                           {true iff $s$ and $s_1$ are not equal}
  116. \smallskip
  117. \+\opb bool      <  s_1  
  118.                           {true iff $s$ is lexicographically smaller than $s_1$}
  119. \smallskip
  120. \+\opb bool      >  s_1  
  121.                           {true iff $s$ is lexicographically greater than $s_1$}
  122. \smallskip
  123. \+\opb bool      <= s_1  
  124.                           {returns $(s < s_1)\ ||\ (s == s_1)$}
  125. \smallskip
  126. \+\opb bool      >= s_1  
  127.                           {returns $(s > s_1)\ ||\ (s == s_1)$}
  128. \smallskip
  129. \+\ops ostream\&  <<  O  
  130.                           { writes string $s$ to the output stream $O$ }
  131. \smallskip
  132. \+\ops istream\&  >>  I  
  133.                           { read($I$,'\ ') }
  134.  
  135. \bigskip
  136. {\bf 3. Implementation }
  137. \smallskip
  138. Strings are implemented by \CC character vectors. All operations on a
  139. string $s$ take time $O(s.length())$.
  140.  
  141.  
  142.